package cn.micro.keep.accounts.controller;


import cn.micro.keep.accounts.api.Result;
import cn.micro.keep.accounts.common.utils.SecurityUtil;
import cn.micro.keep.accounts.entity.AccountDimShareGroup;
import cn.micro.keep.accounts.entity.AccountOdsShareGroupApplyJoinRecord;
import cn.micro.keep.accounts.entity.dto.*;
import cn.micro.keep.accounts.mapstruct.AccountDimShareGroupMapstruct;
import cn.micro.keep.accounts.service.AccountDimShareGroupService;

import cn.micro.keep.accounts.service.AccountOdsShareGroupApplyJoinRecordService;
import cn.micro.keep.accounts.service.AccountOdsShareGroupRequestJoinRecordService;
import cn.micro.keep.accounts.util.ValidateGroup;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 分享组信息表 前端控制器
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-08
 */
@RestController
@RequestMapping("/accounts/accountDimShareGroup")
@RequiredArgsConstructor
@Slf4j
@Validated
public class AccountDimShareGroupController {

    private final AccountDimShareGroupService accountDimShareGroupService;

    private final AccountOdsShareGroupApplyJoinRecordService shareGroupApplyJoinRecordService;

    private final AccountOdsShareGroupRequestJoinRecordService shareGroupRequestJoinRecordService;

    /**
     * 检索群列表
     * @param search
     * @return
     */
    @GetMapping("list")
    public Object list(AccountDimShareGroupSearch search){
        return new Result<>().success(new PageInfo<>(accountDimShareGroupService.listByUser(search)));
    }

    @GetMapping("listByUser")
    public Object listByUser(AccountDimShareGroupSearch search){
        search.setUserId(SecurityUtil.getUserId());
        return new Result<>().success(new PageInfo<>(accountDimShareGroupService.listByUser(search)));
    }

    @PostMapping("create")
    public Object create(@Validated(value = ValidateGroup.add.class) @RequestBody AccountDimShareGroupDTO dto){
        if (!accountDimShareGroupService.create(dto)){
            return new Result<>().fail("创建失败");
        }
        return new Result<>().success("创建成功");
    }

    @PutMapping("edit")
    public Object edit(@Validated(value = ValidateGroup.edit.class) @RequestBody AccountDimShareGroupDTO dto){
        //修改的名称不能已存在的
        LambdaQueryWrapper<AccountDimShareGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountDimShareGroup::getName,dto.getName())
                .ne(AccountDimShareGroup::getId,dto.getId())
               .eq(AccountDimShareGroup::getCreatorId,SecurityUtil.getUserId());
        if (accountDimShareGroupService.getOne(queryWrapper)!=null){
            return new Result<>().fail("该分享群名称已存在");
        }
        AccountDimShareGroup shareGroup = new AccountDimShareGroup();
        AccountDimShareGroupMapstruct.INSTANCE.dtoToEntity(dto,shareGroup);
        if (!accountDimShareGroupService.updateById(shareGroup)){
            return new Result<>().fail("修改失败");
        }
        return new Result<>().success("修改成功");
    }

    @PostMapping("applyJoinGroup")
    public Object applyJoinGroup(@Validated @RequestBody ShareGroupApplyDTO dto){
        AccountOdsShareGroupApplyJoinRecord applyJoinRecord = new AccountOdsShareGroupApplyJoinRecord();
        applyJoinRecord.setShareGroupId(dto.getShareGroupId());
        applyJoinRecord.setApplyUserId(SecurityUtil.getUserId());
        applyJoinRecord.setApplyDesc(dto.getApplyDesc());
        if (!shareGroupApplyJoinRecordService.save(applyJoinRecord)){
            return new Result<>().fail("申请失败");
        }
        return new Result<>().success("申请成功");
    }

    @PostMapping("requestJoinGroup")
    public Object requestJoinGroup(@Validated @RequestBody ShareGroupRequestDTO dto){
        if (!shareGroupRequestJoinRecordService.requestJoinGroup(dto)){
            return new Result<>().fail("邀请失败");
        }
        return new Result<>().success("邀请成功");
    }

    @PutMapping("approveJoinGroup")
    public Object approveJoinGroup(@Validated @RequestBody ApproveJoinGroupDTO dto){
        if (!shareGroupApplyJoinRecordService.approveJoinGroup(dto)){
            return new Result<>().fail("审批失败");
        }
        return new Result<>().success("审批成功");
    }

    /**
     * 回复邀请
     * @param dto
     * @return
     */
    @PutMapping("replyJoinGroup")
    public Object replyJoinGroup(@Validated @RequestBody ReplyJoinGroupDTO dto){
        if (!shareGroupRequestJoinRecordService.replyJoinGroup(dto)){
            return new Result<>().fail();
        }
        return new Result<>().success();
    }

    /**
     * 被邀请请求列表
     * @param search
     * @return
     */
    @GetMapping("listAcceptRequestJoinGroup")
    public Object listAcceptRequestJoinGroup(RequestJoinGroupSearch search){
        return new Result<>().success(new PageInfo<>(shareGroupRequestJoinRecordService.listAcceptRequestJoinGroup(search)));
    }

    /**
     * 申请加入群记录列表
     * @param search
     * @return
     */
    @GetMapping("listApplyJoinGroup")
    public Object listApplyJoinGroup(ApplyJoinGroupSearch search){
        return new Result<>().success(new PageInfo<>(shareGroupApplyJoinRecordService.listApplyJoinGroup(search)));
    }

    /**
     * 退出群
     * @param id
     * @return
     */
    @DeleteMapping("exitGroup/{id}")
    public Object exitGroup(@PathVariable Long id){
        if (!accountDimShareGroupService.exitGroup(id)){
            return new Result<>().fail("退出失败");
        }
        return new Result<>().success("退出成功");
    }

    /**
     * 解散群---只有群主才有权限进行解散
     * @param dto
     * @return
     */
    @PutMapping("dismissGroup")
    public Object dismissGroup(BaseDTO dto){
        if (!accountDimShareGroupService.dismissGroup(dto)){
            return new Result<>().fail("解散失败");
        }
        return new Result<>().success("解散失败");
    }


}
